package com.jcx.ldzj.utile;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.io.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;


public class JsonTest {
    //读取json文件
    public static String readJsonFile(String fileName) {
        String jsonStr = "";
        try {
            File jsonFile = new File(fileName);
            FileReader fileReader = new FileReader(jsonFile);
            Reader reader = new InputStreamReader(new FileInputStream(jsonFile),"utf-8");
            int ch = 0;
            StringBuffer sb = new StringBuffer();
            while ((ch = reader.read()) != -1) {
                sb.append((char) ch);
            }
            fileReader.close();
            reader.close();
            jsonStr = sb.toString();
            return jsonStr;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 缓存Map
     */
    private static Map<String,String> keyMap=new HashMap<>();
    private static Map<String,String> valueMap=new HashMap<>();

//    public static void main(String[] args) {
//        Resource resource = new ClassPathResource("json/tail.properties");
//        Properties props = new Properties();
//        try {
//             props = PropertiesLoaderUtils.loadProperties(resource);
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        Object[] objects = props.stringPropertyNames().toArray();
//        for (int i = 0; i < objects.length; i++) {
//            String key = objects[i].toString();
//            keyMap.put(key,props.getProperty(key));
//            valueMap.put(props.getProperty(key),key);
//        }
//        String path = JsonTest.class.getClassLoader().getResource("json/fast.json").getPath();
//        String s = readJsonFile(path);
//        JSONObject jobj = JSON.parseObject(s);
//        System.out.printf( changeJsonObj(jobj).toString());
//    }

    /**
     * 递归遍历替换字段
     * @param jsonObj
     * @return
     */
    public static JSONObject changeJsonObj(JSONObject jsonObj) {
        JSONObject resJson = new JSONObject();
        Set<String> keySet = jsonObj.keySet();
        for (String key : keySet) {
            String resKey = keyMap.get(key) == null ? key : keyMap.get(key);
            try {
                if (jsonObj.get(key) instanceof JSONObject) {
                    JSONObject jsonobj1 = (JSONObject) jsonObj.get(key);
                    resJson.put(resKey, changeJsonObj(jsonobj1));
                } else if (jsonObj.get(key) instanceof JSONArray) {
                    JSONArray jsonArr = (JSONArray) jsonObj.get(key);
                    resJson.put(resKey, changeJsonArr(jsonArr));
                }else {
                    resJson.put(resKey, jsonObj.get(key));
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return resJson;
    }

    public static JSONArray changeJsonArr(JSONArray jsonArr) {
        JSONArray resJson = new JSONArray();
        for (int i = 0; i < jsonArr.size(); i++) {
            JSONObject jsonObj = jsonArr.getJSONObject(i);
            resJson.add(changeJsonObj(jsonObj));
        }
        return resJson;
    }
}